package cn.edu.cqut.test0514;

public class Test3 {
	public static void main(String[] args) {
		Product product = new Product();
		new Thread(() -> {
			for (;;) {
				product.produce();
				try {
					Thread.sleep(1000);
				} catch (Exception e) {
					;
				}
			}
		}, "甲").start();
//		new Thread(()->{},"乙").start();
//		new Thread(()->{},"丙").start();
		new Thread(() -> {
			for (;;) {
				product.consume();
//				try {
//					Thread.sleep(1000);
//				} catch (Exception e) {
//					;
//				}
			}
		}, "A").start();
//		new Thread(()->{},"B").start();
//		new Thread(()->{},"C").start();
	}
}

class Product extends Object {
	private int num = 0;

	synchronized public void produce() {
		if (num == 1)
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		num++;
		System.out.println(Thread.currentThread().getName() + ": 0->1");
		notify();
	}

	synchronized public void consume() {
		if (num == 0)
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		num--;
		System.out.println(Thread.currentThread().getName() + ": 1->0");
		notify();
	}
}
